var base64 = new Base64();
var dialog = new auiDialog();
var order = new Vue({
    el: '#app',
    data() {
        return {
            list: [],
            curOrder: {},
            orderStatus: {
                PENDING: 0,
                CHECK_PENDING: 1,
                UNACCEPTED: 2,
                ACCEPTED: 3,
                TRADE_SUCCESS: 4,
                TRADE_CLOSE: 5,
                WAIT_FOR_SERVICE: 6
            },
            allturnover: 0.0,
            todayturnover: 0.0,
            todayquantity: 0,
            headimgurl: '',
            remark: '车主更改时间'
        }
    },
    methods: {
        getUnacceptedOrderList: function() {
            params = new Object();
            params.technician = api.getGlobalData({
                key: 'technicianid'
            });
            params.status = this.orderStatus.UNACCEPTED;
            params.asc = false; // false:未接的单放在最前面    true：已处理过的单子放在最前面
            order.findOrderList(params, function(ret, err) {
                // console.log('ret' + JSON.stringify(ret));
                order.list = ret;
                console.log('list:' + order.list.length);
                //隐藏加载页面
                api.hideProgress();
            });
        },
        getAcceptedOrderList: function() {
            params = new Object();
            params.technician = api.getGlobalData({
                key: 'technicianid'
            });
            params.status = this.orderStatus.ACCEPTED + ',' + this.orderStatus.WAIT_FOR_SERVICE;
            params.asc = true; // false:未接的单放在最前面    true：已处理过的单子放在最前面
            order.findOrderList(params, function(ret, err) {
                // console.log('ret' + JSON.stringify(ret));
                order.list = ret;
                console.log('list:' + order.list.length);
                //隐藏加载页面
                api.hideProgress();
            });
        },
        login: function(openid, callback) {
            $api.ajax(function(ret, err) {
                if (ret) {
                    api.setGlobalData({
                        key: 'openid',
                        value: ret.openid
                    });
                    api.setGlobalData({
                        key: 'userheadimgurl',
                        value: ret.technician.headimgurl
                    });
                    api.setGlobalData({
                        key: 'technicianInfo',
                        value: ret.technician
                    });
                    if (ret.technician == '') {
                        //隐藏加载页面
                        api.hideProgress();
                        alert('还未注册，请先注册。');
                        $api.openWin('register');
                    } else {
                        technician.info = ret.technician
                        console.log('=login=' + JSON.stringify(technician.info));
                        callback(ret, err);
                    }
                } else {}
            }, 'technician/login', 'post', openid);
        },         
        hasLogin: function(callback) {
            var technicianid = api.getGlobalData({
                key: 'technicianid'
            });
            if (technicianid != '') {
                callback();
            } else {
                var openid = api.getGlobalData({
                    key: 'useropenid'
                });                        
                if (openid != ''){
                    this.login(openid, function(ret, err) {
                        api.setGlobalData({
                            key: 'technicianid',
                            value: ret.technician.id
                        });
                        order.hasAcceptedOrderList(function() {
                            order.getUnacceptedOrderList(); //初始化
                        });
                    });
                } else {
                    api.setFrameGroupIndex({
                        name: 'IndexGroup',
                        index: 2,
                        reload: true
                    }); 
                } 
                /* api.setFrameGroupIndex({
                    name: 'IndexGroup',
                    index: 2,
                    reload: true
                }); */
            }
        },
        hasAcceptedOrderList: function(callback) {
            var params = {}
            params.technician = api.getGlobalData({
                key: 'technicianid'
            });
            params.status = this.orderStatus.ACCEPTED + ',' + this.orderStatus.WAIT_FOR_SERVICE;
            params.asc = true;
            $api.ajax(function(ret, err) {
                if (ret.length > 0) {
                    alert('您还有进行中的订单，请先处理进行中的订单。');
                    api.setFrameGroupIndex({
                        name: 'IndexGroup',
                        index: 3,
                        reload: true
                    });
                } else {
                    callback();
                }
            }, 'order/findOrderList', 'post', params);
        },
        openDialog: function(curOrder) {
            // console.log('==openDialog' + curOrder);
            dialog.alert({
                title: "确定要撤销吗?",
                msg: '撤销后就无法继续洗车了哦',
                buttons: ['取消', '确定撤销']
            }, function(ret) {
                if (ret.buttonIndex == 2) {
                    // console.log('==openDialog' + JSON.stringify(curOrder));
                    api.setGlobalData({
                        key: 'curOrder',
                        value: curOrder
                    });
                    $api.openWin('cancelledorder');
                }
                // console.log(ret.buttonIndex)
            });
        },
        startWach: function(curOrder) {
            api.setGlobalData({
                key: 'curOrder',
                value: curOrder
            });
            $api.openWin('upphoto');
        },
        endWach: function(curOrder) {
            api.setGlobalData({
                key: 'curOrder',
                value: curOrder
            });
            $api.openWin('upphoto_complete');
        },
        openMap: function(coordinate, address) {
            //  alert(coordinate);
            var bMap = api.require('bMap');
            var melon = '';
            var melat = '';
            try {
                bMap.getLocation({
                    accuracy: '100m',
                    autoStop: true,
                    filter: 1
                }, function(ret, err) {
                    if (ret.status) {
                        melon = ret.lon;
                        melat = ret.lat;
                        var tempStr = coordinate;
                        var tempArr = tempStr.split(",");
                        //  order.coordinate.lon = tempArr[0];
                        //  order.coordinate.lat = tempArr[1];
                        //  order.coordinate.address = address;
                        var destination = tempArr[1] + ',' + tempArr[0];
                        var origin = melat + ',' + melon;
                        //  console.log(' destination: '+destination + ' origin: '+origin);
                        api.openApp({
                            androidPkg: 'android.intent.action.VIEW',
                            appParam: {
                                origin: 'latlng:' + origin,
                                destination: 'latlng:' + destination, //'latlng:26.085013,119.286097',
                                region: '',
                                mode: 'driving',
                                src: api.appName
                            },
                            mimeType: 'text/html',
                            uri: 'intent://map/direction?origin=' + origin + '&destination=' + destination + '&mode=driving&src=osm#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end',
                            iosUrl: 'baidumap://map/direction'
                        }, function(ret, err) {
                            if (ret) {
                                //alert(JSON.stringify(ret));
                            } else {
                                api.toast({
                                    msg: "请先安装百度地图app",
                                    location: 'middle'
                                })
                            }
                        });
                    } else {
                        alert(err.code);
                    }
                });
            } catch (err) {
                alert('错误:' + err);
            }
        },
        acceptOrder: function(curorder) {
            order.curOrder = curorder;
            order.curOrder.technician = api.getGlobalData({
                key: 'technicianid'
            });
            order.curOrder.status = order.orderStatus.WAIT_FOR_SERVICE;
            console.log('curOrdernow: ' + JSON.stringify(order.curOrder));
            //  console.log('curorder:'+JSON.stringify(order.curOrder ));
            if (curorder) {
                order.updateOrder(order.curOrder, function(ret, err) {
                    console.log('ret' + JSON.stringify(ret));
                    // console.log('errupdate: '+err);
                    api.setGlobalData({
                        key: 'curOrder',
                        value: ret
                    });
                    //启动订单坐标上传开关
                    api.setGlobalData({
                        key: 'isorder',
                        value: true
                    });
                    //发送订单完成事件
                    api.sendEvent({
                        name: 'orderaccept',
                        extra: {
                            msg: '订单完成'
                        }
                    });
                    api.setFrameGroupIndex({
                        name: 'IndexGroup',
                        index: 3,
                        reload: true
                    });
                });
            }
        },
        findOrderList: function(params, callback) {
            $api.ajax(function(ret, err) {
                //  console.log('ret' + JSON.stringify(ret));
                // console.log('err' + JSON.stringify(err));
                if (ret) {
                    this.list = ret;
                    callback(ret, err);
                }
            }, 'order/findOrderList', 'post', params);
        },
        updateOrder: function(order, callback) {
            $api.ajax(function(ret, err) {
                if (ret) {
                    callback(ret, err);
                }
            }, 'order/updateOrder', 'post', order);
        },
        updateReason: function(shipping, callback) {
            $api.ajax(function(ret, err) {
                if (ret) {
                    callback();
                }
            }, 'shipping/update', 'post', shipping);
        },
        selectReason: function(reson) {
            order.remark = reson;
        },
        openwinHere: function(name) {
            var delay = 0;
            if (api.systemType != 'ios') {
                delay = 300;
            }
            api.openWin({
                name: '' + name + '',
                url: '' + name + '.html',
                bounces: false,
                delay: delay,
                slidBackEnabled: true,
                vScrollBarEnabled: false
            });
        },
        cancelOrder() {
            var curorder = api.getGlobalData({
                key: 'curOrder'
            });
            curorder.technician = '0';
            curorder.status = this.orderStatus.UNACCEPTED;
            curorder.technicianInfo = {};
            console.log('=curorder=' + JSON.stringify(curorder));
            var shipping = curorder.technicianList[0];
            console.log('=shipping=' + JSON.stringify(shipping));
            shipping.remark = order.remark;
            console.log('=shipping=' + JSON.stringify(shipping));
            order.updateReason(shipping, function() {
                order.updateOrder(curorder, function(ret, err) {
                    console.log('cancelOrder' + JSON.stringify(ret));
                    //发送订单完成事件
                    api.sendEvent({
                        name: 'orderaccept',
                        extra: {
                            msg: '订单完成'
                        }
                    });
                    api.closeToWin({
                        name: 'root'
                    });
                })
            });
        },
        call(phoneno) {
            api.call({
                type: 'tel_prompt',
                number: '' + phoneno + ''
            });
        }
    }
});
